<%@ page isELIgnored="false" %><%--isELIgnored=true禁用EL表达式--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- JSTL标签库是一个JSP标签的集合，封装了许多jsp应用程序通用的核心功能-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui-v2.6.13/layui/css/layui.css"/>
    <script src="${pageContext.request.contextPath}/js/jquery.min-2.1.1.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui-v2.6.13/layui/layui.js"></script>
    <style>
        .layui-row{
            margin-bottom: 30px;
        }
        .layui-form{
            margin-left: 20px;
            margin-top: 20px;
        }
        #d1{
            margin-left: 20px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<c:if test="${gStatus == 1}">
    <form class="layui-form" action="http://localhost:8080/shopping/goods/queryUpGoods">
</c:if>
<c:if test="${gStatus == 0}">
    <form class="layui-form" action="http://localhost:8080/shopping/goods/queryDownGoods">
</c:if>
    <div class="layui-row">
        <!-- layui-input-block：是一个表单元素占一行，竖着，以例表的形式排列。
            layui-input-inline：多个表单控件占一行，横着，一行可以放置多个表单元素。 -->
        <div class="layui-input-inline">
            <input type="text" name="gName" placeholder="请输入查询商品名称"  class="layui-input" value="${goods.gName}">
        </div>

        <div class="layui-input-inline">
            <select name="parentId" lay-filter="f1">
                <option value="">一级分类</option>
                <c:forEach items="${first}" var="first">
                    <option
                        <c:if test="${goods.parentId == first.id}"> selected=true </c:if>
                       value="${first.id}">${first.sName}</option>
                </c:forEach>
            </select>
        </div>
        <div class="layui-input-inline">
            <select name="sId" id="second">
                <option value=''>二级分类</option>
                <c:forEach items="${second}" var="second">
<%--                    <c:if test="${goods.parentId == second.parentId}">--%>
                        <option
                                <c:if test="${goods.sId == second.id}"> selected=true </c:if>
                                value="${second.id}">${second.sName}</option>
<%--                    </c:if>--%>
                </c:forEach>
            </select>
        </div>
        <!-- layui-btn 把标签作为按钮
            layui-btn-sm 小型按钮 -->
        <button class="layui-btn">查询</button>
    </div>

</form>
<div id="d1">
    <c:if test="${gStatus == 1}">
        <button class="layui-btn" type="button" onclick="downGoods()">一键下架</button>
    </c:if>
    <c:if test="${gStatus == 0}">
        <button class="layui-btn" type="button" onclick="upGoods()">一键上架</button>
    </c:if>
    <!-- <table class="layui-table" lay-even lay-skin="nob">
            无边框表格（内部结构参见右侧目录“常规用法”）
        </table> -->
    <table class="layui-table">
        <!-- colgroup中可以使用col来定义表格单元格宽度， -->
        <colgroup>
            <col width="50">
            <col width="200">
            <col width="200">
            <col width="300">
            <col width="300">
            <col width="300">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th><input type="checkbox" name="allsel" lay-skin="primary" onclick="checkAll()"></th>
            <th>商品名称</th>
            <th>商品图片</th>
            <th>库存</th>
            <th>单价</th>
            <th>所属分类</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${list}" var="goods">
            <tr>
                <td><input type="checkbox" name="gStatus" id="i1" lay-skin="primary" value="${goods.id}"></td>
                <td>${goods.gName}</td>
                <td><img src="${goods.imgUrl}"></td>
                <td>${goods.gCount}</td>
                <td>${goods.gPrice}</td>
                <td>${goods.sName}</td>
                <td><button type="button" class="layui-btn" onclick="update('${goods.id}')">更新</button></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <div id="test1" style="text-align: center;"></div>
    <div style="text-align: center;">
        <c:if test="${gStatus == 1}">
            <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="queryUpByPageNO(1,${totalPage})">首页</button>
            <button class="layui-btn" onclick = "queryUpByPageNO(${pageNo-1},${totalPage})">上一页</button>
            <span class = "layui-font-16">${pageNo} / ${totalPage}页</span>
            <button class="layui-btn" onclick = "queryUpByPageNO(${pageNo+1},${totalPage})">下一页</button>
            <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="queryUpByPageNO(${totalPage},${totalPage})">尾页</button>
        </c:if>
        <c:if test="${gStatus == 0}">
            <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="queryDownPageNO(1,${totalPage})">首页</button>
            <button class="layui-btn" onclick = "queryDownPageNO(${pageNo-1},${totalPage})">上一页</button>
            <span class = "layui-font-16">${pageNo} / ${totalPage}页</span>
            <button class="layui-btn" onclick = "queryDownPageNO(${pageNo+1},${totalPage})">下一页</button>
            <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="queryDownPageNO(${totalPage},${totalPage})">尾页</button>
        </c:if>
    </div>
</div>
</body>
<script>
    function update(id){
        layer.open({
            type: 2,
            area: ['750px', '400px'],
            title: "修改货物页面",
            content: 'http://localhost:8080/shopping/goods/updateGoodsPage?&id='+id,
            yes: function(index, layero) {
                layer.close(index);
            },
        })
    }
    function queryUpByPageNO(pageNo,totalPage){
        if(pageNo <= totalPage){
            //查询条件
             var params = $("form").serialize();
            if(pageNo < 1) pageNo = 1;
            window.location.href = "http://localhost:8080/shopping/goods/queryUpGoods?"+params
                +"&pageNo="+pageNo;
            // window.location.href = "http://localhost:8080/shopping/manager/queryAllAdmin?"+params+"&pageNo="+pageNo;
        }
    }
    function queryDownPageNO(pageNo,totalPage){
        if(pageNo <= totalPage){
            //查询条件
             var params = $("form").serialize();
            if(pageNo < 1) pageNo = 1;
            window.location.href = "http://localhost:8080/shopping/goods/queryDownGoods?"+params
                +"&pageNo="+pageNo;
        }
    }
    function upGoods(){
// var arr = [];	//声明一个数组用来存放遍历出来的checkbox value值
        // $("input[name='productState']:checked").each(function(i) {	//遍历
        //     arr.push("productId=" + $(this).val());	//将我们遍历出来的值push到数组中
        //     //最后可以提交arr就可以实现将我们选中的checkbox的value值提交
        //
        // });
        var userList = new Array();
        $("input[name='gStatus']:checked").each(function(i){
            var i = parseInt($(this).val());
            userList.push(i);
        });
        console.info(userList);
        $.ajax({
            url:"http://localhost:8080/shopping/goods/UpGoods",
            data:{ids : userList},
            type:"get",
            dataType:"json",
            success(obj){
                if(obj == "1"){
                    alert("上架成功");
                    window.location.reload();
                }else {
                    alert("上架失败");
                    window.location.reload();
                }
            }
        });
    }
    function downGoods(){
        var userList = new Array();
        $("input[name='gStatus']:checked").each(function(i){
            var i = parseInt($(this).val());
            userList.push(i);
        });
        console.info(userList);
        $.ajax({
            url:"http://localhost:8080/shopping/goods/DownGoods",
            data:{ids : userList},
            type:"get",
            dataType:"json",
            success(obj){
                if(obj == "1"){
                    alert("下架成功");
                    window.location.reload();
                }else {
                    alert("下架失败");
                    window.location.reload();
                }
            }
        });
    }
    function checkAll(){
        var obj = $("input[name='allsel']").prop("checked");
        if (obj === true){
            $("input[name='gStatus']:not(:checked)").each(function() {
                this.checked = true;
            });
        }
        if (obj === false){
            $("input[name='gStatus']:checked").each(function() {
                this.checked = false;
            });
        }
    }
    layui.use(['layer','form'], function(){
        var layer = layui.layer;
        var form = layui.form;
        form.on('select(f1)', function (data) {
            console.info(data.elem); //得到select原始DOM对象
            console.info(data.value); //得到被选中的值

            var params = "parentId=" + data.value;
            $.ajax({
                url: "http://localhost:8080/shopping/goods/querySecond",
                data: params,
                type: "post",
                success: function (obj) {
                    //将json字符串转为数组
                    var arr = JSON.parse(obj);

                    //清空子元素
                    $("#second").empty();
                    $("#second").append("<option value=''>二级分类</option>");
                    //添加子元素
                    $.each(arr, function (index, item) {
                        console.info(item);
                        //在元素内部末尾插入子元素
                        $("#second").append(
                            "<option value='" + item.id + "'>" + item.name
                            + "</option>");
                    });
                    //重新渲染select下拉框
                    form.render('select');
                }
            });
        });
    });
</script>
</html>